package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        String email = user.getEmail();
        //检查邮箱是否已经存在
        User temp = userMapper.selectByEmailAndPassword(email, null);
        if(temp!=null){
            throw new RuntimeException("邮箱已被注册!");
        }
        //设置默认头像和背景
        user.setHeadImgUrl("/img/user/head.jpg");
        user.setCoverImgUrl("/img/user/bg.jpeg");
        userMapper.insert(user);
    }

    @Override
    public User login(User user) {
        if(StringUtils.hasLength(user.getEmail())&&StringUtils.hasLength(user.getPassword())){
            User temp = userMapper.selectByEmailAndPassword(user.getEmail(), user.getPassword());
            if(temp==null){
                throw new RuntimeException("邮箱或密码错误!");
            }
            //把user放到session中
            UserContext.setCurrentUser(temp);
            return temp;
        }
        throw new RuntimeException("邮箱或密码为空!");
    }

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void follow(Long starId, int num) {
        Long followId = UserContext.getCurrentUser().getId();
        System.out.println(followId);
        //关注者 关注数+1 或 -1
        //被关者 粉丝数+1 或 -1
        userMapper.updateFollowNum(followId, num);
        userMapper.updateFansNum(starId, num);
        //维护关注者和被关者的关系
        if(num == 1){
            userMapper.saveFollowStarRelation(followId, starId);
        }else if(num == -1){
            userMapper.deleteFollowStarRelation(followId, starId);
        }
    }

    @Override
    public boolean judgeFollow(Long starId) {
        Long followId = UserContext.getCurrentUser().getId();
        //通过被访问者的id, 查询他所有的粉丝
        List<Long> fansIds = userMapper.selectFansIdsByStarId(starId);
        //如果包含当前登录的用户返回true
        return fansIds.contains(followId);
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> getFollows(Long id) {
        if (id != null) {
            //查询用户所关注人的id
            Long[] ids = userMapper.selectFollowsById(id);
            System.out.println(Arrays.toString(ids));
            //通过id,查询对象
            if (ids!= null && ids.length!=0) {
                return userMapper.selectUsersByIds(ids);
            }
        }
        return null;
    }

    @Override
    public User selectById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }


}
